<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Usuarios
 *
 * @author Ramiro
 */
class Token {

    public function __construct() {
        
    }

    private function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }

    static public function getInstancia() {
        if (self::$instancia == NULL) {
            self::$instancia = new Connection();
        }
        return self::$instancia;
    }

    public function escapar($var) {
        $var = stripslashes($var);
        $var = htmlentities($var);
        $var = strip_tags($var);
        $var = addslashes($var);
        $var = mysql_real_escape_string($var);
        return $var;
    }

    public function maxId() {
        $SQL = 'SELECT  max(`id`) as id FROM `tokens` ';
        $con = Connection::getInstancia();
        $result = $con->obtenerQuery($SQL);
        if (!$result) {
            die('Could not run query: ' . mysql_error());
        }
        $row = mysql_fetch_assoc($result);
        if ($row) {
            return $row['id'];
        }
        else
            return 0;
    }

    public function updateToken($token) {
        $token = $this->escapar($token);
        $con = ConnectionSecure::getInstancia();
        $con->conectar();
        $dbconn = $con->getConexion();
        $query = $dbconn->prepare('UPDATE `tokens` SET usado=1 WHERE `Token`= ?');
        $query->bind_param("s", $token);
        if ($query->execute() == true) {
            $con->desconectar();
            return true;
        } else {
            $con->desconectar();
            return false;
        }
    }

    public function cuentaTieneToken($idcuenta, $token) {
        $idcuenta = $this->escapar($idcuenta);
        $token = $this->escapar($token);
        //`idcuenta`  =' . $idcuenta . '  AND 
        $SQL = 'SELECT  `id` FROM `tokens` WHERE  `idcuenta`  =' . $idcuenta . '  AND  `Token`=\'' . $token . '\' AND NOT `usado` LIMIT 1;';
        $con = Connection::getInstancia();
        $result = $con->obtenerQuery($SQL);
        if (!$result) {
            die('Could not run query: ' . mysql_error());
        }
        $row = mysql_fetch_assoc($result);
        if ($row) {
            return $row['id'];
        }
        else
            return '-1';
    }
    
    public function tieneSaldo($origen,$monto) {
        $origen = $this->escapar($origen);
        //`idcuenta`  =' . $idcuenta . '  AND 
        $SQL = 'SELECT  `id` FROM `cuenta` WHERE  `id`  =' . $origen . '  AND  `Saldo`> ' .$monto.';';
       
        $con = Connection::getInstancia();
        $result = $con->obtenerQuery($SQL);
        if (!$result) {
            die('Could not run query: ' . mysql_error());
        }
        $row = mysql_fetch_assoc($result);
        if ($row) {
            return $row['id'];
        }
        else
            return '-1';
    }

}

?>
